home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware in MacFormat / brailler0.5b / brlr ƒ / Shell ƒ / menus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-20  |  10.6 KB  |  436 lines  |  [TEXT/MMCC]

  1. #include "menus.h"
  2. #include "graphics.h"
  3. #include "help.h"
  4. #include "environment.h"
  5. #include "print meat.h"
  6. #include "brlr load-save.h"
  7. #include "brlr.h"
  8. #include "text twiddling.h"
  9. #include "graphics dispatch.h"
  10. #include "window layer.h"
  11. #include "program globals.h"
  12.  
  13. static    MenuHandle        gAppleMenu;
  14. static    MenuHandle        gFileMenu;
  15. static    MenuHandle        gEditMenu;
  16. static    MenuHandle        gOptionsMenu;
  17.  
  18. enum
  19. {
  20.     appleMenu = 400, fileMenu, editMenu, optionsMenu,
  21.     
  22.     aboutItem = 1, aboutMSGItem, otherMSGItem, helpPointerItem,
  23.     
  24.     newItem = 1, openItem, file_unused1, closeItem, saveItem, saveAsItem, file_unused2,
  25.         pageSetupItem, printItem, file_unused3, quitItem,
  26.     
  27.     undoItem = 1, edit_unused0, cutItem, copyItem, pasteItem, clearItem, edit_unused1, selectAllItem,
  28.     
  29.     useBrailleItem = 1, useLettersItem, useGrade2Item
  30. };
  31.  
  32. /*-----------------------------------------------------------------------------------*/
  33. /* internal stuff for menus.c                                                        */
  34.  
  35. static    void HandleAppleMenu(short menuItem);
  36. static    void HandleFileMenu(short menuItem);
  37. static    void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough);
  38. static    void HandleOptionsMenu(short menuItem);
  39. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition);
  40. static    MenuHandle GetIndMenuHandle(short index);
  41. static    short GetIndMenuID(short index);
  42.  
  43. Boolean InitTheMenus(void)
  44. {
  45.     Handle            MBARHandle;
  46.     
  47.     if ((MBARHandle=GetNewMBar(400))==0L)        /* sez which menus are in menu bar. */
  48.         return FALSE;
  49.     SetMenuBar(MBARHandle);                        /* set this to be THE menu bar to use. */
  50.     
  51.     if ((gAppleMenu=GetMHandle(appleMenu))==0L)    /* GetNewMBar also got menu handles of */
  52.         return FALSE;
  53.     if ((gFileMenu=GetMHandle(fileMenu))==0L)    /* every menu it includes, so just */
  54.         return FALSE;
  55.     if ((gEditMenu=GetMHandle(editMenu))==0L)    /* grab these handles and assign them */
  56.         return FALSE;
  57.     if ((gOptionsMenu=GetMHandle(optionsMenu))==0L)
  58.         return FALSE;
  59.     
  60.     AddResMenu(gAppleMenu, 'DRVR');                /* adds control panels to apple menu */
  61.     
  62.     AdjustMenus();                                /* dim/enable/check/mark menus/items */
  63.     DrawMenuBar();                                /* draws the actual menu bar */
  64.     
  65.     return TRUE;
  66. }
  67.  
  68. void AdjustMenus(void)
  69. {
  70.     short            kind;
  71.     WindowPtr        front, frontDoc, mainWindow;
  72.     
  73.     front=FrontWindow();
  74.     kind=front ? ((WindowPeek)front)->windowKind : 0;
  75.     frontDoc=GetFrontDocumentWindow();
  76.     mainWindow=GetIndWindowPtr(kMainWindow);
  77.     
  78.     EDItem(gAppleMenu, 0, TRUE);
  79.     EDItem(gAppleMenu, aboutItem, TRUE);
  80.     EDItem(gAppleMenu, aboutMSGItem, TRUE);
  81.     EDItem(gAppleMenu, otherMSGItem, TRUE);
  82.     EDItem(gAppleMenu, helpPointerItem, TRUE);
  83.     
  84.     EDItem(gFileMenu, 0, TRUE);
  85.     EDItem(gFileMenu, newItem, mainWindow==0L);
  86.     EDItem(gFileMenu, openItem, mainWindow==0L);
  87.     EDItem(gFileMenu, closeItem, front!=0L);
  88.     EDItem(gFileMenu, saveItem, (mainWindow!=0L) && (WindowHasLayer(mainWindow)) &&
  89.         (WindowIsModifiedQQ(mainWindow)));
  90.     EDItem(gFileMenu, saveAsItem, mainWindow!=0L);
  91.     EDItem(gFileMenu, pageSetupItem, TRUE);
  92.     EDItem(gFileMenu, printItem, (frontDoc!=0L) && (WindowHasLayer(frontDoc)) &&
  93.         (GetWindowTE(frontDoc)!=0L));
  94.     EDItem(gFileMenu, quitItem, TRUE);
  95.     
  96.     EDItem(gEditMenu, 0, TRUE);
  97.     EDItem(gEditMenu, undoItem, kind<0);
  98.     EDItem(gEditMenu, cutItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyHighlightedQQ(frontDoc)));
  99.     EDItem(gEditMenu, copyItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyHighlightedQQ(frontDoc)));
  100.     EDItem(gEditMenu, pasteItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyTextInScrapQQ()));
  101.     EDItem(gEditMenu, clearItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyHighlightedQQ(frontDoc)));
  102.     EDItem(gEditMenu, selectAllItem, (frontDoc!=0L) && (frontDoc==mainWindow) && (AnyTextQQ(frontDoc)));
  103.     
  104.     EDItem(gOptionsMenu, 0, TRUE);
  105.     EDItem(gOptionsMenu, useBrailleItem, mainWindow!=0L);
  106.     EDItem(gOptionsMenu, useLettersItem, mainWindow!=0L);
  107.     EDItem(gOptionsMenu, useGrade2Item, mainWindow!=0L);
  108.     
  109.     SetItemMark(gOptionsMenu, useBrailleItem, (gGrade==0) ? '◊' : noMark);
  110.     SetItemMark(gOptionsMenu, useLettersItem, (gGrade==1) ? '◊' : noMark);
  111.     SetItemMark(gOptionsMenu, useGrade2Item, (gGrade==2) ? '◊' : noMark);
  112. }
  113.  
  114. void HandleMenu(long mSelect)
  115. {
  116.     short            menuID = HiWord(mSelect);
  117.     short            menuItem = LoWord(mSelect);
  118.     
  119.     switch (menuID)
  120.     {
  121.         case appleMenu:
  122.             HandleAppleMenu(menuItem);
  123.             break;
  124.         case fileMenu:
  125.             HandleFileMenu(menuItem);
  126.             break;    
  127.         case editMenu:
  128.             HandleEditMenu(menuItem, FALSE);
  129.             break;
  130.         case optionsMenu:
  131.             HandleOptionsMenu(menuItem);
  132.             break;
  133.     }
  134. }
  135.  
  136. void DoTheCloseThing(WindowPeek theWindow)
  137. /* a standard close procedure, called when "close" is chosen from File menu and when
  138.    a window is closed through its close box */
  139. {
  140.     short            kind;
  141.     
  142.     if (theWindow==0L)
  143.         return;
  144.     
  145.     kind = theWindow ? theWindow->windowKind : 0;
  146.     if (kind<0)        /* DA window or other system window */
  147.         CloseDeskAcc(kind);
  148.     else
  149.     {
  150.         if (WindowHasLayer((WindowPtr)theWindow))
  151.             CloseTheWindow((WindowPtr)theWindow);
  152.         else
  153.             DisposeWindow((WindowPtr)theWindow);
  154.         
  155.         AdjustMenus();
  156.     }
  157. }
  158.  
  159. void HandleAppleMenu(short menuItem)
  160. {
  161.     GrafPtr        savePort;
  162.     Str255        name;
  163.     
  164.     switch (menuItem)
  165.     {
  166.         case aboutItem:
  167.             if (!IndWindowExistsQQ(kAboutWindow))
  168.                 OpenTheIndWindow(kAboutWindow);
  169.             else
  170.                 MySelectWindow(GetIndWindowPtr(kAboutWindow));
  171.             break;
  172.         case aboutMSGItem:
  173.             if (!IndWindowExistsQQ(kAboutMSGWindow))
  174.                 OpenTheIndWindow(kAboutMSGWindow);
  175.             else
  176.                 MySelectWindow(GetIndWindowPtr(kAboutMSGWindow));
  177.             break;
  178.         case otherMSGItem:
  179.             if (!IndWindowExistsQQ(kOtherMSGWindow))
  180.                 OpenTheIndWindow(kOtherMSGWindow);
  181.             else
  182.                 MySelectWindow(GetIndWindowPtr(kOtherMSGWindow));
  183.             break;
  184.         case helpPointerItem:
  185.             if (!IndWindowExistsQQ(kHelpWindow))
  186.                 OpenTheIndWindow(kHelpWindow);
  187.             else
  188.                 MySelectWindow(GetIndWindowPtr(kHelpWindow));
  189.             break;
  190.         default:
  191.             if (menuItem > helpPointerItem+1)
  192.             {
  193.                 GetPort(&savePort);
  194.                 GetItem(gAppleMenu, menuItem, name);
  195.                 OpenDeskAcc(name);
  196.                 SetPort(savePort);
  197.             }
  198.             break;
  199.     }
  200. }
  201.  
  202. void HandleFileMenu(short menuItem)
  203. {
  204.     WindowPtr            theWindow;
  205.     TEHandle            hTE;
  206.     
  207.     switch (menuItem)
  208.     {
  209.         case newItem:
  210.             OpenTheIndWindow(kMainWindow);
  211.             break;
  212.         case openItem:
  213.             LoadSaveDispatch(TRUE, FALSE);
  214.             break;
  215.         case closeItem:
  216.             if ((theWindow=GetFrontDocumentWindow())!=0L)
  217.                 DoTheCloseThing((WindowPeek)theWindow);
  218.             else
  219.                 DoTheCloseThing((WindowPeek)FrontWindow());
  220.             break;
  221.         case saveItem:
  222.             LoadSaveDispatch(FALSE, TRUE);
  223.             break;
  224.         case saveAsItem:
  225.             LoadSaveDispatch(FALSE, FALSE);
  226.             break;
  227.         case pageSetupItem:
  228.             RemoveHilitePatch();
  229.             DoThePageSetup();
  230.             InstallHilitePatch();
  231.             break;
  232.         case printItem:
  233.             theWindow=GetFrontDocumentWindow();
  234.             if (WindowHasLayer(theWindow))
  235.             {
  236.                 hTE=GetWindowTE(theWindow);
  237.                 if (hTE!=0L)
  238.                 {
  239.                     RemoveHilitePatch();
  240.                     PrintText(hTE);
  241.                     InstallHilitePatch();
  242.                 }
  243.             }
  244.             else SysBeep(7);
  245.             break;
  246.         case quitItem:
  247.             gDone=ShutDownTheProgram();
  248.             break;
  249.     }
  250. }
  251.  
  252. void HandleEditMenu(short menuItem, Boolean alreadyPassedThrough)
  253. {
  254.     short            index;
  255.     WindowPtr        frontDoc;
  256.     enum DispatchError    resultCode;
  257.     
  258.     if (!alreadyPassedThrough)
  259.         index=(gFrontWindowIsOurs) ? gFrontWindowIndex : -1;
  260.     else
  261.     {
  262.         frontDoc=GetFrontDocumentWindow();
  263.         index=(frontDoc!=0L) ? GetWindowIndex(frontDoc) : -1;
  264.     }
  265.     
  266.     if (index>=0)
  267.     {
  268.         switch (menuItem)
  269.         {
  270.             case undoItem:        resultCode=UndoDispatch(index);            break;
  271.             case cutItem:        resultCode=CutDispatch(index);            break;
  272.             case copyItem:        resultCode=CopyDispatch(index);            break;
  273.             case pasteItem:        resultCode=PasteDispatch(index);        break;
  274.             case clearItem:        resultCode=ClearDispatch(index);        break;
  275.             case selectAllItem:    resultCode=SelectAllDispatch(index);    break;
  276.             default:            resultCode=kSuccess;                    break;
  277.         }
  278.         
  279.         if ((resultCode==kPassThrough) && (!alreadyPassedThrough))
  280.             HandleEditMenu(menuItem, TRUE);
  281.     }
  282.     else SystemEdit(menuItem-1);
  283. }
  284.  
  285. void HandleOptionsMenu(short menuItem)
  286. {
  287.     switch (menuItem)
  288.     {
  289.         case useBrailleItem:
  290.             gGrade=0;
  291.             if (IndWindowExistsQQ(kFloatingWindow))
  292.                 CloseTheWindow(GetIndWindowPtr(kFloatingWindow));
  293.             break;
  294.         case useLettersItem:
  295.             gGrade=1;
  296.             if (IndWindowExistsQQ(kFloatingWindow))
  297.                 CloseTheWindow(GetIndWindowPtr(kFloatingWindow));
  298.             break;
  299.         case useGrade2Item:
  300.             gGrade=2;
  301.             if (!IndWindowExistsQQ(kFloatingWindow))
  302.                 OpenTheIndWindow(kFloatingWindow);
  303.             break;
  304.     }
  305.     
  306.     AdjustMenus();
  307. }
  308.  
  309. static    void EDItem(MenuHandle theMenu, short theItem, Boolean theCondition)
  310. {
  311.     if (theCondition)
  312.         EnableItem(theMenu, theItem);
  313.     else
  314.         DisableItem(theMenu, theItem);
  315. }
  316.  
  317. typedef    pascal void (*mdef_function)(short, MenuHandle, Rect*, Point, short*);
  318.  
  319. struct privateQDGlobals
  320. {
  321.     char privates[76];
  322.     long randSeed;
  323.     BitMap screenBits;
  324.     Cursor arrow;
  325.     Pattern dkGray;
  326.     Pattern ltGray;
  327.     Pattern gray;
  328.     Pattern black;
  329.     Pattern white;
  330.     GrafPtr thePort;
  331.     long    end;
  332. };
  333.  
  334. typedef struct privateQDGlobals privateQDGlobals;
  335.  
  336. pascal long ManualMenuSelect(short menuIndex)
  337. {
  338.     mdef_function    *theMDEFHandle;
  339.     mdef_function    theMDEF;
  340.     MenuHandle        theMenu;
  341.     Rect            menuRect, drawRect;
  342.     Point            thePoint;
  343.     short            item;
  344.     short            i;
  345.     short            menuOffset;
  346.     
  347.     short            oldMenuBarHeight;
  348.     long            oldA5;
  349.     privateQDGlobals        qd;                /* our QD globals. */
  350.     GrafPort        gp;                /* our grafport. */
  351.     GrafPtr            savePort;
  352.     
  353.     HiliteMenu(GetIndMenuID(menuIndex));
  354.     theMenu=GetIndMenuHandle(menuIndex);
  355.     
  356.     theMDEFHandle=(mdef_function*)((**theMenu).menuProc);
  357.     theMDEF=*theMDEFHandle;
  358.     SetPt(&thePoint, 0, 0);
  359.     TextFont(0);
  360.     TextSize(12);
  361.     TextFace(0);
  362.     menuOffset=30;
  363.     for (i=2; i<menuIndex; i++)
  364.     {
  365.         menuOffset+=StringWidth((**GetIndMenuHandle(i)).menuData);
  366.         menuOffset+=StringWidth("\p   ");
  367.     }
  368.     
  369.     GetPort(&savePort);
  370.     oldMenuBarHeight=LMGetMBarHeight();
  371.     LMSetMBarHeight(0);
  372.     DrawMenuBar();
  373.  
  374.     /* get a value for A5, a structure that mirrors qd globals. */
  375.     oldA5 = SetA5((long)&qd.end);
  376.     InitGraf(&qd.thePort);
  377.     OpenPort(&gp);
  378.     
  379.     SetRect(&menuRect, 0, 0, 0, 0);
  380.     theMDEF(mSizeMsg, theMenu, &menuRect, thePoint, &item);
  381.     
  382.     menuRect.left=menuOffset;
  383.     menuRect.right=menuRect.left+(**theMenu).menuWidth;
  384.     menuRect.top=0;
  385.     menuRect.bottom=menuRect.top+(**theMenu).menuHeight;
  386.     
  387.     drawRect=menuRect;
  388.     drawRect.top=LMGetMBarHeight();
  389.     drawRect.bottom=drawRect.top+(**theMenu).menuHeight;
  390. //    drawRect.left-=5;
  391.     drawRect.right+=5;
  392.     EraseRect(&drawRect);
  393.     FrameRect(&drawRect);
  394.     item=0;
  395.     SetPt(&thePoint, 0, 50);
  396.     theMDEF(mDrawMsg, theMenu, &menuRect, thePoint, &item);
  397.     item=0;
  398.     
  399.     while (Button()) {};
  400.     
  401.     LMSetMBarHeight(oldMenuBarHeight);
  402.     DrawMenuBar();
  403.     
  404.     ClosePort(&gp);
  405.     SetA5(oldA5);
  406.     SetPort(savePort);
  407.  
  408.     return 0L;
  409. }
  410.  
  411. static    MenuHandle GetIndMenuHandle(short index)
  412. {
  413.     switch (index)
  414.     {
  415.         case 1:    return gAppleMenu;
  416.         case 2:    return gFileMenu;
  417.         case 3:    return gEditMenu;
  418.         case 4:    return gOptionsMenu;
  419.     }
  420.     
  421.     return 0L;
  422. }
  423.  
  424. static    short GetIndMenuID(short index)
  425. {
  426.     switch (index)
  427.     {
  428.         case 1:    return 400;
  429.         case 2:    return 401;
  430.         case 3:    return 402;
  431.         case 4:    return 403;
  432.     }
  433.     
  434.     return 0;
  435. }
  436.